package com.ouheng.shop.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ouheng.shop.dao.CarriageSetDao;
import com.ouheng.shop.domain.CarriageSetDO;
import com.ouheng.shop.domain.ProductPriceDO;
import com.ouheng.shop.service.CarriageSetService;



@Service
public class CarriageSetServiceImpl implements CarriageSetService {
	@Autowired
	private CarriageSetDao carriageSetDao;
	
	@Override
	public CarriageSetDO get(Long id){
		return carriageSetDao.get(id);
	}
	
	@Override
	public List<CarriageSetDO> list(Map<String, Object> map){
		return carriageSetDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return carriageSetDao.count(map);
	}
	
	@Override
	public int save(CarriageSetDO carriageSet){
		List<CarriageSetDO> listCarriageSet = carriageSetDao.list(null);
		if(listCarriageSet.isEmpty()){
			carriageSet.setStatus("2");
		}else{
			if("2".equals(carriageSet.getStatus())){
				Map<String, Object> map = new HashMap<>();
				map.put("status", 2);
				List<CarriageSetDO> listCarriageSetDO = carriageSetDao.list(map);
				for (CarriageSetDO carriageSetDO : listCarriageSetDO) {
					carriageSetDO.setStatus("1");
					carriageSetDao.update(carriageSetDO);
				}
			}
		}
		return carriageSetDao.save(carriageSet);
	}
	
	@Override
	public int update(CarriageSetDO carriageSet){
		if("2".equals(carriageSet.getStatus())){
			Map<String, Object> map = new HashMap<>();
			map.put("status", 2);
			List<CarriageSetDO> list = carriageSetDao.list(map);
			if(!list.isEmpty()){
				for (CarriageSetDO carriageSetDO : list) {
					carriageSetDO.setStatus("1");
					carriageSetDao.update(carriageSetDO);
				}
			}
		}
		return carriageSetDao.update(carriageSet);
	}
	
	@Override
	public int remove(Long id){
		return carriageSetDao.remove(id);
	}
	
	@Override
	public int batchRemove(Long[] ids){
		return carriageSetDao.batchRemove(ids);
	}
	
}
